package com.quzhi.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quzhi.common.code.Code;
import com.quzhi.common.Result;
import com.quzhi.sys.entity.OrderRemark;
import com.quzhi.sys.mapper.OrderRemarkMapper;
import com.quzhi.sys.service.IOrderRemarkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Xibing
 * @since 2024-07-03
 */
@Service
public class OrderRemarkServiceImpl extends ServiceImpl<OrderRemarkMapper, OrderRemark> implements IOrderRemarkService {

    public Result<?> addOrderRemark(OrderRemark orderRemark) {
        // 查询所有备注，如果该手机号存在则提示该手机号备注已存在
        LambdaQueryWrapper<OrderRemark> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderRemark::getPhone, orderRemark.getPhone());

        OrderRemark one = this.baseMapper.selectOne(wrapper);
        if(one != null) return Result.fail(Code.FAIL_CODE, "该手机号备注已经添加过了");

        // 设置ID 创建时间，更新时间
        orderRemark.setId(UUID.randomUUID().toString());
        orderRemark.setCreateTime(LocalDateTime.now());
        orderRemark.setUpdateTime(LocalDateTime.now());
        this.baseMapper.insert(orderRemark);
        return Result.success("添加成功");
    }

    @Override
    public Result<?> updateOrderRemark(OrderRemark orderRemark) {
        // 查询所有备注，如果该手机号存在则提示该手机号备注已存在
        orderRemark.setUpdateTime(LocalDateTime.now());
        this.baseMapper.updateById(orderRemark);

        return Result.success("更新成功");
    }
}
